<?php

namespace App\Shop\Controllers;

use App\Models\Shop;
use App\Shop\Models\Administrator;
use Dcat\Admin\Form;
use Dcat\Admin\Http\Controllers\AuthController as BaseAuthController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class AuthController extends BaseAuthController
{

    /**
     * Handle a login request.
     *
     * @param  Request  $request
     * @return mixed
     */
    public function postLogin(Request $request)
    {
        $credentials = $request->only([$this->username(), 'password']);
        $remember = (bool) $request->input('remember', false);

        /** @var \Illuminate\Validation\Validator $validator */
        $validator = Validator::make($credentials, [
            $this->username()   => 'required',
            'password'          => 'required',
        ]);

        if ($validator->fails()) {
            return $this->validationErrorsResponse($validator);
        }

        if ($this->guard()->attempt($credentials, $remember)) {
            $user = $this->guard()->user();
            if ($user->status==2){
                return $this->response()->error("此店主账号已被禁用!");
            }
            return $this->sendLoginResponse($request);
        }
        return $this->validationErrorsResponse([
            $this->username() => $this->getFailedLoginMessage(),
        ]);
    }

    /**
     * Model-form for user setting.
     *
     * @return Form
     */
    protected function settingForm()
    {
        return new Form(new Administrator(), function (Form $form) {
            $form->action(admin_url('auth/setting'));
            $form->disableCreatingCheck();
            $form->disableEditingCheck();
            $form->disableViewCheck();


            $form->tools(function (Form\Tools $tools) {
                $tools->disableView();
                $tools->disableDelete();
            });

            $form->column(6,function (Form $form){
                $form->display('username', trans('admin.username'));
                $form->display('name','店铺名称');
                $form->display('fullname','姓名');
//                $form->display('id_card','身份证号');
                $form->display('phone','手机号');
                $form->password('old_password', trans('admin.old_password'));

                $form->password('password', trans('admin.password'))
                    ->minLength(5)
                    ->maxLength(20)
                    ->customFormat(function ($v) {
                        if ($v == $this->password) {
                            return;
                        }

                        return $v;
                    });
                $form->password('password_confirmation', trans('admin.password_confirmation'))->same('password');

                $form->ignore(['password_confirmation', 'old_password']);
            });

            $form->column(6,function (Form $form){
                $form->image('avatar', trans('admin.avatar'))->autoUpload();
                $form->radio('status','状态')->options(['营业中','暂停营业','已结业'])->default(0);
            });




            $form->saving(function (Form $form) {
                if ($form->password && $form->model()->password != $form->password) {
                    $form->password = bcrypt($form->password);
                }

                if (! $form->password) {
                    $form->deleteInput('password');
                }
            });

            $form->saved(function (Form $form) {
                return $form
                    ->response()
                    ->success(trans('admin.update_succeeded'))
                    ->redirect('auth/setting');
            });
        });
    }
}
